#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <openssl/hmac.h>
#include <openssl/evp.h>

int main(int argc, char *argv[])
{
	int fd, status;

	if (argc < 3) {
		printf
		    ("usage : ./test_caphash <userid1@userid2> <randomstring> \n");
		exit(-1);
	}

	/* Hash the capability */
	HMAC_CTX hctx;
	char *sigptr;
	unsigned int outputlen;
	unsigned char *key = argv[2];
	unsigned char output[20], *buf;
	char *input = argv[1];

	HMAC_CTX_init(&hctx);
	HMAC_Init(&hctx, key, strlen(key), EVP_sha1());
	HMAC(EVP_sha1(), key, strlen(key),
	     (unsigned char *) input, strlen(input), output, &outputlen);
	unsigned int t = outputlen;

	buf = output;
	printf("hash of the capability being written to /dev/caphash :\n");
	while (t--)
		printf("%02x", *buf++);
	printf("\n");

	fd = open("/dev/caphash", O_RDWR);
	if (fd < 0)
		perror("error opening the file");

	status = write(fd, output, outputlen);
	if (status < 0)
		perror("error writing to the file");

	printf("Capability successfully written to the /dev/caphash \n");
	return 0;
}
